package com.starsoft.smdc.web;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.starsoft.frame.core.orm.PageObject;
import com.starsoft.frame.core.web.WebUtil;
import com.starsoft.frame.util.StringUtil;
import com.starsoft.smdc.bean.SmdcEr;
import com.starsoft.smdc.bean.SmdcErId;
import com.starsoft.smdc.bean.SmdcSecurity;
import com.starsoft.smdc.crawler.SmdcUpdateService;
import com.starsoft.smdc.service.ErService;
import com.starsoft.smdc.service.SecurityService;

@RestController
@RequestMapping(value = "/smdc/er")
public class ErController {

	@Autowired
	ErService erService;
	
	@Autowired
	SecurityService securityService;
	
	@Autowired
	SmdcUpdateService updateService;
	
	@RequestMapping(value="/page")
	public Map<String, Object> pageQuery(HttpServletRequest request){
		PageObject po = WebUtil.getPageObject(request);
		String secId = request.getParameter("secId");
		Date beginDate = WebUtil.getDate(request, "beginDate", "yyyy-MM-dd");
		Date endDate = WebUtil.getDate(request, "endDate", "yyyy-MM-dd");
		return erService.pageQuery(po, secId, beginDate, endDate);
	}
	
	@RequestMapping(value="/update")
	public Map<String, Object> update(HttpServletRequest request){
		int num=0;
		String secId = request.getParameter("secId");
		if(StringUtil.isEmpty(secId)){
			num = updateService.updateErAll().size();
		}else{
			SmdcSecurity security = securityService.get(secId);
			Date beginDate = WebUtil.getDate(request, "beginDate", "yyyy-MM-dd");
			if(beginDate==null){
				beginDate = erService.getBeginDate(security);
			}
			num = updateService.updateEr(security, beginDate).size();
		}
		return WebUtil.sucesseModel("更新成功:"+num);
	}
	
	@RequestMapping(value="/delete")
	public Map<String, Object> delete(HttpServletRequest request, SmdcEr er){
		Date reportDate = WebUtil.getDate(request, "reportDate", "yyyy-MM-dd");
		String secId = request.getParameter("secId");
		SmdcErId id = new SmdcErId(secId, reportDate);
		er.setId(id);
		erService.delete(er);
		return WebUtil.sucesseModel("删除成功");
	}
	
	@RequestMapping(value="/save")
	public Map<String, Object> save(SmdcEr er, HttpServletRequest request){
		Date reportDate = WebUtil.getDate(request, "reportDate", "yyyy-MM-dd");
		String secId = request.getParameter("secId");
		SmdcErId id = new SmdcErId(secId, reportDate);
		er.setId(id);
		erService.save(er);
		return WebUtil.sucesseModel("保存成功");
	}
	
	@RequestMapping(value="/datayesupload")
	public Map<String, Object> datayesUpload(@RequestParam(value = "file") MultipartFile file) throws IOException {
		return WebUtil.errorModel("Not support");
	}
}
